from django.http import Http404
from django.template import RequestContext
from django.contrib.sites.models import Site
from django.conf import settings
from django.shortcuts import redirect, render_to_response
from page.models import *
from configurator.models import *
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.decorators import login_required

def admin_login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                if request.GET.has_key('next'):
                    return redirect(request.GET['next'])
                else:
                    return redirect(settings.ADMIN_HOME_PAGE)
            else:
                return redirect(settings.LOGIN_URL)
        else:
            redirect(settings.LOGIN_URL)
        form = AuthenticationForm(request.POST)
    else:
        form = AuthenticationForm()

    template = 'admin/login.html'
    context = {
        'form': form,
    }
    return render_to_response(template, context, context_instance = RequestContext(request))

def admin_logout(request):
    logout(request)
    return redirect(settings.LOGIN_URL)

@login_required
def index(request):
    template = 'admin/index.html'

    context = {}
    return render_to_response(template, context, context_instance = RequestContext(request))

